import { HealthCheckWithScreenTask, V3proLastErrCache } from "./health.check.class";
import { SendboxKey, V3proSubjectMessage } from "../model/v3pro.model";
import { HealthDescEnum, V3proAlertMessageErrTypeEnum } from "../model/v3pro.enum";
import { V3proAllStatusLambda } from "../helper/v3pro.all.status.lambda.helper";
import { V3proScreenLayoutHelper } from "../helper/v3pro.screen.layout.helper";
import { V3proScreen, V3proScreenAllStatus } from "../../../api/v3pro-api/v3pro.api.response.model";
import { appConfig } from "../../../configs/env.config.load";

/**
 * 发送盒温度异常
 *
 * 是否支持阈值设置： Y
 * [75°,100°]
 * 默认85°
 *
 * 查询周期:
 * 定时查询
 * 间隔15秒
 * 连续3次
 *
 * 告警产生的原因:
 * 发送盒工作温度超出阈值
 *
 * 异常现象:
 * 发送盒带载区域花屏
 * 发送盒带载区域黑屏
 *
 * 维修建议:
 * 依次排查环境温度、发送盒硬件
 *
 * 告警级别: 次要
 */

const errCache = new V3proLastErrCache<SendboxKey>();
export class V3proCheckSendboxTemperatureTask extends HealthCheckWithScreenTask<SendboxKey[]> {
  async checkResult(
    keyParams: SendboxKey[],
    screenId: string,
    allStatus: V3proScreenAllStatus,
  ): Promise<V3proSubjectMessage> {
    const message = new V3proSubjectMessage();
    if (keyParams.length === 0) return message;
    message.setErrStatus();
    return message;
  }

  async genKeyParams(
    allStatus: V3proScreenAllStatus,
    screen: V3proScreen,
    screenLayoutHelper: V3proScreenLayoutHelper,
  ): Promise<SendboxKey[]> {
    const [errs, normals] = V3proAllStatusLambda.filterSendBoxesWithRemove(
      allStatus,
      V3proAllStatusLambda.healthDescrWithStatusEquals(HealthDescEnum.OVER_TEMP),
    );
    return errCache.mergeCache(
      errs.map(SendboxKey.build),
      normals.map(SendboxKey.build),
      screen.id,
      (s) => s.id,
    );
  }

  genSRMqttTopic(
    message: V3proSubjectMessage,
    screenId: string,
    allStatus: V3proScreenAllStatus,
  ): string {
    return "发送盒温度异常";
  }

  getV3proAlertMessage(): V3proAlertMessageErrTypeEnum {
    return V3proAlertMessageErrTypeEnum.SEND_BOX_TEMP;
  }

  isNeedUploadFile(
    message: V3proSubjectMessage,
    screenId: string,
    allStatus: V3proScreenAllStatus,
  ): boolean {
    return false;
  }

  isReportEnabled(keyParams: any, screenId: string): boolean {
    return appConfig.v3pro.errEnable.temperature;
  }
}
